package pw.hais.zongcaibook.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.squareup.okhttp.Response;

import pw.hais.http.base.OnHttpListener;
import pw.hais.utils.EmptyUtil;
import pw.hais.utils.L;
import pw.hais.zongcaibook.R;
import pw.hais.zongcaibook.app.BaseActivity;
import pw.hais.zongcaibook.dao.LeadDao;
import pw.hais.zongcaibook.entity.MessageEntity;
import pw.hais.zongcaibook.model.LeadModel;

/**
 * Created by hais1992 on 2016/1/24/024.
 */
public class FeedbackActivity extends BaseActivity implements View.OnClickListener {
    private EditText edit_title,edit_username,edit_content;
    private Button btn_push;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);

        edit_title = (EditText) findViewById(R.id.edit_title);
        edit_username = (EditText) findViewById(R.id.edit_username);
        edit_content = (EditText) findViewById(R.id.edit_content);
        btn_push = (Button) findViewById(R.id.btn_push);
        btn_push.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_push:
                if(verificationInfo()){
                    doPushMssage();
                }
                break;
        }
    }

    private String title,username,content;
    private boolean verificationInfo() {
        username = edit_username.getText() == null ? "" : edit_username.getText().toString().trim();
        title = edit_title.getText() == null ? "" : edit_title.getText().toString().trim();
        content = edit_content.getText() == null ? "" : edit_content.getText().toString().trim();

        if(EmptyUtil.emptyOfString(content) && EmptyUtil.emptyOfString(title)){
            L.showShort("内容 和 标题 至少填写一栏！");
        }else{
            return true;
        }
        return false;
    }

    private void doPushMssage() {
        MessageEntity messageEntity = new MessageEntity();
        messageEntity.title = title;
        messageEntity.username = username;
        messageEntity.content = content;
        loadDialogShow("留言中...");
        LeadDao.postFeedback(messageEntity, new OnHttpListener<LeadModel>() {
            @Override
            public void onSuccess(Response response, LeadModel data) {
                loadDialogDismiss();
                finish();
                L.showShort("留言成功，感谢您的反馈！");
            }
        });
    }
}
